﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Bll;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;

/// <summary>
/// 产品检验项目模板文件上传。
/// </summary>
public class UploadProduct : IUploadFile
{
    private DaProductCheckAbility daProduct = DevFuture.Common.WorkContext.AppCtx.GetObject("DaProductCheckAbility") as DaProductCheckAbility;
    private DaTfiles datfile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;

    public UploadProduct()
    { }

    #region IUploadFile 成员

    public void InitUpload(IUploadPage up, System.Collections.Specialized.NameValueCollection nvc)
    {
        #region 为产品检验项上传附件
        //给定产品检验项的EID，给定通知标题，给定返回URL
        string strPcaId = nvc["PCAID"];//获取产品检验项ID号
        string strType = nvc["templateType"];//获取产品检验项模板类型。
        up.SetConfigValue("FILETYPE", strType);

        if (String.IsNullOrEmpty(strPcaId) || strPcaId.Trim().Length == 0)
        {
            Utility.SiteError("缺失所必需的参数【产品检验项编号】", up.Response);
        }
        else
        {
            up.SetConfigValue("PCAID", strPcaId.Trim());

            MoProductCheckAbility mo = new MoProductCheckAbility();

            mo.PcaId = strPcaId;
            daProduct.GetEntityEx(ref mo);

            string strTransType = string.Empty;
            switch (strType)
            { 
                case "1":
                    strTransType = "参数模板";
                    break;
                case "2":
                    strTransType = "报告Word模板（全）";
                    break;
                case "3":
                    strTransType = "Excel模板1";
                    break;
                case "4":
                    strTransType = "Word模板1";
                    break;
                case "5":
                    strTransType = "Excel模板2";
                    break;
                case "6":
                    strTransType = "Word模板2";
                    break;
                default:
                    Utility.SiteError("必需的参数【产品检验项模板类型】为空。", up.Response);
                    return;
            }
            up.SetPageTitle(String.Format("为产品检验项【{0}】【{1}>{2}>{3}】上传附件({4})：",
                mo.PcaId, mo.Pname, mo.Pmodel, mo.Ptype, strTransType));
        }
        #endregion
    }

    public int Upload1(IUploadPage up, MoTfiles file, string opter_id)
    {

        MoRefFile modref = new MoRefFile();
        modref.Eid = up.GetConfigValue("PCAID").ToString();
        modref.FileId = file.FileId;
        modref.RfId = SequenceKeys.GenerateReferFileID();
        try
        {
            return datfile.AddFileProduct(file, modref,modref.Eid,up.GetConfigValue("FILETYPE").ToString());
        }
        catch (DalException de)
        {
            Utility.GlobalLog.Error("添加单个文件失败!", de);
            Utility.SiteError("添加单个文件失败!", up.Response);
            return -1;
        }
    }

    public int UploadedMuliti(IUploadPage up, List<MoTfiles> files, string opter_id)
    {
        throw new Exception("The method or operation is not implemented.");
    }

    public string GetReturnUrl(IUploadPage up)
    {
        return String.Format("notice.aspx?edit&noticeid={0}", up.GetConfigValue("NOTICE_ID"));
    }

    #endregion
}
